package utl.mofen.handler;

//{ 
//    "params":
//     {
//    	"id":"12010101",              
//    	"ver":"",
//     },    
//    "plugin":
//     {
//       "name":"configLoader"
//     }
//}
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;


import utl.mofen.delphi.cds.Util;
import utl.mofen.impl.DBOperator;
import utl.mofen.intf.IConfigLoader;
import utl.mofen.intf.IDBOperator;
import utl.mofen.jsonStreamHandler.JSonStreamHandler;
import utl.mofen.script.SQLParser;
import utl.mofen.script.ScriptLoader;
import utl.mofen.script.ScriptObjectFactory;
import utl.mofen.utils.GsonUtil;
import utl.mofen.utils.StringUtil;

public class ConfigLoaderHandler extends JSonStreamHandler {
	public void doAction() throws Exception {
		JsonObject json = this.jsonStream.getJSonObject();
		
		IConfigLoader sl = new ScriptLoader(JSonConfigReader.getdbid(json, "sys"));
		
		if (!json.has("params"))
		{
			throw new Exception("没有指定获取配置的参数!");
		}
		
		JsonObject params = json.get("params").getAsJsonObject();
		
		if (params.has("isStream")&&params.get("isStream").getAsBoolean())
		{
			this.jsonStream.setStream(sl.getData(params.get("id").getAsString(), 
					JSonConfigReader.getVersion(json)));			
		}else
		{
			String decode="";
			if (params.has("decode"))
			{
				decode = params.get("decode").getAsString();				
			}
			String data = sl.getString(params.get("id").getAsString(),
					JSonConfigReader.getVersion(json), decode);
			
			if (JSonConfigReader.getIsTrace(json))
			{
				System.out.println(data);
				JSonConfigWriter.writeTrace(this.jsonStream, data, "configs");				
			}
			
			this.jsonStream.setStream(data.getBytes("utf-8"));
		}

	}
}
